package com.jszc.lottery.common.datatest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;


public class HttpClient {

   private static final Logger logger = LoggerFactory.getLogger(HttpClient.class);

    /**
     * 返回服务器提交数据
     *
     * @return
     */
    public static String http(String url) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection urlConnection = realUrl.openConnection();
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            urlConnection.setRequestProperty("accept", "*/*");
            urlConnection.setRequestProperty("connection", "Keep-Alive");
            urlConnection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
//            out = new PrintWriter(urlConnection.getOutputStream());
//            out.print(postdata);
//            out.flush();   //刷脏数据
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line = "";
            while ((line = in.readLine()) != null) {
                result += line;
            }
            return result;
        } catch (Exception e) {
            logger.error("HttpClient.http error", e);
            return null;
        } finally {
            if (out != null) {
                out.close();   //关闭输出流
            }

            if (in != null) {      //关闭写入流
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    /**
     *多个参数处理
     * @param fg
     * @param paramter
     * @return
     */
    public static String implode(String fg,String[] paramter){
        StringBuffer sb = new StringBuffer();
        sb. append(paramter[0]);
        for(int i = 1; i < paramter.length; i++){
            sb. append(fg+paramter[i]);
        }
        return sb.toString();
    }
}
